#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 520;

int n, m;
int g[N][N];

int f(int k){
    if(k == 0) return 0;
    if(k == 1) return 1;
    if(k >= 2) return 2;

}


int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        int sumnum = 0;
        int maxv = 0;
        cin >> n >> m;
        for(int i = 1; i <= n; i ++){
            string s;
            cin >> s;
            for(int j = 1; j <= m; j ++){
                g[i][j] = s[j - 1] - '0';
                if(g[i][j]) sumnum ++;
            }
        }

        for(int i = 1; i <= n - 1; i ++){
            for(int j = 1; j <= m - 1; j ++){
                int k = g[i][j] + g[i + 1][j] + g[i][j + 1] + g[i + 1][j + 1];
                maxv = max(maxv, sumnum - k + f(k));
            }
        }

        cout << maxv << '\n';



    }


    return 0;
}